package app.server.project.core.json;

import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import lombok.Data;

import java.util.HashMap;

@Data
public abstract class JsonLife {

    private HashMap<Object, Object> cacheData = new HashMap<>();

    public <T> T getById(Object id) {
        return (T) this.cacheData.get(id);
    }


    public void init(HashMap<Object, JSONObject> dataMap) {
        this.cacheData.clear();
        dataMap.forEach((k, v) -> {
            var data = JSONUtil.toBean(v, this.getClassType());
            this.cacheData.put(k, data);
        });
    }

    public abstract Class getClassType();

    public abstract void load(HashMap<Object, JSONObject> dataMap);
}
